<?php
/**
 * 用户登录过程
 *
 */
namespace app\member\controller;

use app\member\model\Member as MemberModel;
use Request;
use think\Controller;

class Login extends Controller
{
    /**
     * 用户登录过程
     * @return [type] [description]
     */
    public function index()
    {
        if ($this->request->post()) {
            $post = $this->request->post();

            $result = $this->validate($post, 'app\member\validate\Member.login');
            if (true !== $result) {
                return $this->error($result);
            }

            if (MemberModel::login($post['user'], $post['password'])) {
                //登录数据处理
                return $this->success('登录成功,正在跳转...', 'member/index/index');
            } else {
                return $this->error('用户名不存在,或密码错误');

            }
        }
        echo config('webconfig1.login_time');
        return $this->fetch();
    }

    /**
     * 自定义验证码
     * @return [type] [description]
     */
    public function verify()
    {
        $config = [
            // 验证码字体大小
            'fontSize' => 30,
            // 验证码位数
            'length'   => 4,
            // 关闭验证码杂点
            'useNoise' => false,
        ];
        $captcha          = new \think\captcha\Captcha($config);
        $captcha->fontttf = '5.ttf';
        return $captcha->entry();
    }

    public function auth($value = '')
    {
        return $this->success('帐户异常请联系管理员...', 'Login/index');
    }

    /**
     * 退出登录
     * @return [type] [description]
     */
    public function loginOut()
    {
        session('userid', null);
        session('user_token', null);
        cookie('username', null);
        cookie('uid', null);
        $this->redirect('Login/index');
    }

    /**
     * 用户注册
     * @return [type] [description]
     */
    public function register()
    {
        if ($this->request->post()) {
            $post   = $this->request->post();
            $result = $this->validate($post, 'app\member\validate\Member.register');
            if (true !== $result) {
                return $this->error($result);
            }
            if (true === MemberModel::register($post)) {
                return $this->success('注册成功,正在跳转...', 'Login/index');
            } else {
                return $this->error('用户名不存在,或密码错误');
            }
        }
        return $this->fetch();
    }

    /**
     * 找回密码
     * @return [type] [description]
     */
    public function resetpwd()
    {
        if ($this->request->post()) {
            $post   = $this->request->post();
            $result = $this->validate($post, 'app\member\validate\Member.resetpwd');
            if (true !== $result) {
                return $this->error($result);
            }
            if (true === MemberModel::resetpwd($post)) {
                return $this->success('找回成功,密码已经发回邮箱,正在跳转...', 'Login/index');
            } else {
                return $this->error('用户名或邮箱错误');
            }
        }
        return $this->fetch();
    }

    /**
     * 邮箱中连接验证过程
     * @param  str $email 用户邮件地址
     * @param  str $token token值
     * @return [type]        [description]
     */
    public function pwdtoken($email, $token)
    {

        if (!isset($email) || !isset($token)) {
            return $this->error('url参数错误');
        }

        $result = MemberModel::where('email', Request::param('email/s', '', 'strip_tags'))->find();
        if (!$result) {
            return $this->error('url连接参数错误');
        }

        $checkToken = md5($result['id'] . $result['user_name'] . $result['password']);
        if ($checkToken != $token) {
            return $this->error('url失效');
        }

        if (time() - $result['pwdtime'] > 0.5 * 60 * 60) {
            return $this->error('url连接超时');
        }
        // 赋值（当前作用域）
        session('checkToken', $checkToken);
        $link = \Request::domain() . '/' . url('login/reset', '', false) . "?uid={$result['id']}";
        // 跳转至客户密码重置页面
        header('location:' . $link);
    }

    /**
     * 重设密码验证
     * @param  int $uid 用户id
     * @return [type]      [description]
     */
    public function reset($uid)
    {

        if (!session('?checkToken') || !isset($uid)) {
            return $this->error('页面失效', '');
        }

        $uid    = Request::param('uid/d', '', 'strip_tags');
        $result = MemberModel::where('id', $uid)->find();

        //检查token 时间
        if ((session('checkToken') != md5($result['id'] . $result['user_name'] . $result['password'])) || (time() - $result['pwdtime'] > 0.5 * 60 * 60)) {
            return $this->error('页面失效', '');
        }
        //若果是POST方式提交的话，就是修改密码了，重置密码就是
        if ($this->request->post()) {
            $post = $this->request->post();

            $result = $this->validate($post, 'app\member\validate\Member.scenereset');
            if (true !== $result) {
                return $this->error($result);
            }

            $user = MemberModel::where([['tid', '=', 2], ['id', '=', $result['id']]])->setField('password', $post['password']);
            if ($user) {
                // 删除（当前作用域）
                session('checkToken', null);
                return $this->success('修改密码成功,正在跳转...', 'Login/index');
            }
        }
        return $this->fetch();
    }







    /**
     * 短信找回密码
     * @return [type] [description]
     */
    // public function resetpwdmsm()
    // {
    //     if ($this->request->post()) {
    //         $post   = $this->request->post();
    //         $result = $this->validate($post, 'app\member\validate\Member.resetpwd');
    //         if (true !== $result) {
    //             return $this->error($result);
    //         }
    //         if (true === MemberModel::resetpwd($post)) {
    //             return $this->success('找回成功,密码已经发回邮箱,正在跳转...', 'Login/index');
    //         } else {
    //             return $this->error('用户名或邮箱错误');
    //         }
    //     }
    //     return $this->fetch();
    // }























}
